<!-- 组织架构 -->
<template>
  <div class="departments-container">
    <div class="app-container">
      <!-- el-card 卡片布局(分为头和体2部分)
        第一个子标签就是头(需要设置slot="header"-插槽)
        知识点: vue2.6之前写具名插槽的写法slot="具名插槽名字" 之后v-slot:header, #header
        第二个子标签部分就是身体 -->
      <el-card class="box-card">
        <div slot="header" class="clearfix">
          <!-- el-tab-pane有几个, 就有几个tab标签导航
               el-tab-pane夹着的内容, 就是这个导航对应的内容
               el-card只用到了头(没有体), 体用的是el-tab标签导航的身体内容(一切放在了卡片的头里写)

               el-tabs: Tabs标签页导航
                      v-model: 绑定点击激活的name值
                      @tab-click: tab导航点击事件
               el-tab-pane: 每个tab标签导航
                      label: tab导航显示文字
                      name: 点击关联到v-model的值
           -->
          <el-tabs v-model="activeName" @tab-click="handleClick">
            <el-tab-pane label="组织架构" name="first" class="tab-pane">
              <el-row type="flex" justify="space-between" align="middle" class="department-header">
                <!-- 第一列20份 -->
                <el-col :span="20">
                  <i class="el-icon-s-home" />
                  <span class="company">江苏传智播客教育科技股份有限公司</span>
                </el-col>
                <!-- 第二列4份 -->
                <el-col :span="4">
                  <!-- 在容器范围里, 一行再分2列 -->
                  <el-row type="flex" justify="end">
                    <el-col>负责人</el-col>
                    <el-col>
                      <!-- 这个之前在头部导航右侧下拉菜单讲过 -->
                      <el-dropdown trigger="click">
                        <!-- 下拉菜单文字 -->
                        <span class="el-dropdown-link">
                          操作<i class="el-icon-arrow-down el-icon--right" />
                        </span>
                        <!-- 下拉项 -->
                        <el-dropdown-menu slot="dropdown">
                          <el-dropdown-item @click.native="add()">添加子部门</el-dropdown-item>
                        </el-dropdown-menu>
                      </el-dropdown>
                    </el-col>
                  </el-row>
                </el-col>
              </el-row>
              <el-tree class="departments-tree" :data="treeData" accordion default-expand-all :expand-on-click-node="false" @node-click="handleNodeClick">
                <template #default="{ data }">
                  <el-row type="flex" justify="space-between" align="middle" style="height: 40px; width: 100%;">
                    <!-- 部门名字 -->
                    <el-col :span="20">
                      <span>{{ data.name }}</span>
                    </el-col>
                    <!-- 功能操作区域 -->
                    <el-col :span="4">
                      <el-row type="flex" justify="end">
                        <!-- 两个内容 -->
                        <el-col>{{ data.manager }}</el-col>
                        <el-col>
                          <!-- 下拉菜单 element -->
                          <el-dropdown trigger="click">
                            <span> 操作<i class="el-icon-arrow-down" /> </span>
                            <!-- 下拉菜单 -->
                            <el-dropdown-menu slot="dropdown">
                              <el-dropdown-item @click.native="add(data)">添加子部门</el-dropdown-item>
                              <el-dropdown-item @click.native="edit(data)">编辑部门</el-dropdown-item>
                              <el-dropdown-item v-if="data&&!data.children" @click.native="del(data)">删除部门</el-dropdown-item>
                            </el-dropdown-menu>
                          </el-dropdown>
                        </el-col>
                      </el-row>
                    </el-col>
                  </el-row>
                </template>
              </el-tree>
            </el-tab-pane>
          </el-tabs>
        </div>
      </el-card>
      <!-- 弹出层 -->
      <depart-dialog ref="departDialog" :parent-id="parentId" :is-edit="isEdit" :origin-list="originList" :dialog-visible.sync="showDepartDialog" :employees-list="employeesList" @addDepartEV="addDepartFn" />
    </div>
  </div></template>

<script>
import { departmentsListAPI, getEmployeeSimpleAPI, addDepartmentsAPI, getDepartDetailAPI, updateDepartmentsAPI, delDepartmentAPI } from '@/api'
import { transTree } from '@/utils'
import departDialog from './components/departDialog.vue'

export default {
  name: 'Departments',
  components: {
    departDialog
  },
  data () {
    return {
      activeName: 'first', // 组织架构显示
      showDepartDialog: false, // 控制弹出层显示与隐藏
      treeData: [
        {
          departName: '总裁办',
          name: '孙财'
        },
        {
          departName: '行政部',
          name: '罗晓晓'
        },
        {
          departName: '人事部',
          name: '安小晴',
          children: [
            {
              departName: '财务核算部'
            }
          ]
        },
        {
          departName: '财务部',
          name: '',
          children: [
            {
              departName: '财务核算部'
            },
            {
              departName: '税务管理部'
            },
            {
              departName: '薪资管理部'
            }
          ]
        },
        {
          departName: '技术部',
          name: '',
          children: [
            {
              departName: '前端技术部'
            },
            {
              departName: '后端技术部'
            }
          ]
        },
        {
          departName: '运营部',
          name: ''
        },
        {
          departName: '市场部',
          name: '武高丽',
          children: [
            {
              departName: '市场部1'
            },
            {
              departName: '市场部2'
            }
          ]
        }
      ],
      defaultProps: {
        children: 'children', // 该属性是进行渲染子节点，值对应的是需要渲染数据的属性值
        label: 'departName' // 该属性是进行渲染的节点名，值对应的是需要渲染数据的属性值
      },
      employeesList: [], // 员工新系列表
      clickDepartId: '', // 保存操作对象的id值
      isEdit: false, // 判断是否为修改状态
      originList: [], // 用于弹窗内-校验部门code和name是否重复数据数组
      parentId: '' // 保存操作对象的父级id
    }
  },
  created() {
    // 获取组织架构数据
    this.getEmployeesListFn()
    // 获取部门分类数据
    this.getDepartMentsListFn()
  },
  methods: {
    // 选项卡点击时间
    handleClick (tab, event) {
      console.log(tab, event)
    },
    handleNodeClick (data) {

    },
    // 正文部分-右侧的添加子部门
    add(data) {
      console.log(data)
      this.isEdit = false
      if (data) {
        this.clickDepartId = data.id
        this.parentId = data.pid
      } else {
        this.clickDepartId = ''
        this.parentId = ''
      }
      this.showDepartDialog = true // 弹出层显示
    },
    // 编辑子部分
    async edit(data) {
      this.isEdit = true
      const res = await getDepartDetailAPI(data.id)
      this.clickDepartId = data.id // 编辑的部门id
      this.$refs.departDialog.form = res.data
      this.showDepartDialog = true // 弹出层显示
    },
    // 删除部分
    async del(data) {
      const delRes = await this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch((error) => {
        return error
      })
      console.log(delRes)
      // 取消删除按钮
      if (delRes === 'cancel') return this.$message('您取消了删除')

      // 删除确认按钮
      const delDepartRes = await delDepartmentAPI(data)
      if (!delDepartRes.success) return this.$message.error(delDepartRes.message)
      // 删除成功需要给用户进行提示
      this.$message.success(delDepartRes.message)
      // 重新渲染数据
      this.getDepartMentsListFn()
      // console.log(res)
    },
    // 请求部门分类数据
    async getDepartMentsListFn() {
      const res = await departmentsListAPI()
      console.log(res)
      // 格式化需要传递给子组件的数据
      this.originList = res.data.depts.map(item => (
        {
          id: item.id,
          code: item.code,
          name: item.name,
          pid: item.pid // 下面使用
        }
      ))
      this.treeData = transTree(res.data.depts, '')
    },
    // 获取员工列表方法
    async getEmployeesListFn() {
      const res = await getEmployeeSimpleAPI()
      this.employeesList = res.data
    },
    // 添加子部门  确定添加
    async addDepartFn(formObj) {
      // 编辑状态
      if (this.isEdit) {
        await updateDepartmentsAPI(formObj)
        // 重新渲染数据
        this.getDepartMentsListFn()
      } else {
        // 新增状态
        formObj.pid = this.clickDepartId // 父级id
        await addDepartmentsAPI(formObj)
        // await 后边的代码在await返回成功后才会执行
        // 重新渲染数据
        this.getDepartMentsListFn()
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.box-card {
  padding: 10px 30px;
}

.tab-pane {
  padding: 20px 80px;

  .departments-tree {
    margin: 12px 0;
  }

  .company {
    margin-left: 12px;
    font-weight: bold;
    font-size: 14px;
  }

  .department-header {
    height: 50px;
    display: flex;
    align-items: center;
    border-bottom: 1px solid #ccc;
  }
}
</style>
